package com.gift.controller;

import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.common.TimeHandler;
import com.gift.dao.BankByDealtMapper;
import com.gift.dao.BankCardMapper;
import com.gift.dao.GamesMapper;
import com.gift.pojo.*;
import com.gift.service.DealTableService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Created by Administrator on 2017/12/7.
 */
@Controller
@RequestMapping("dealTable")
public class DealTableController {

    //时间计算
    @RequestMapping("time")
    @ResponseBody
    public ServerResponse gettime(){

        //现在时间
        LocalTime nowtime = LocalTime.now();
        //最小时间
        LocalTime mintime = LocalTime.MIN;
        //最大时间
        LocalTime maxtime = LocalTime.MAX;
        //计算时间，小时
        LocalTime localTime = LocalTime.of(21, 30, 59, 11001);
        //3小时后
        LocalTime localTimeLater   = localTime.plusHours(3);
        //3小时前
        LocalTime localTimeEarlier = localTime.minusHours(3);
        //计算时间，天
        //构造时间
        LocalTime zero = LocalTime.of(0, 0, 0); // 00:00:00
        LocalTime mid = LocalTime.parse("12:00:00"); // 12:00:00

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        String createtime = "";
        createtime = createtime.substring(0,10);
        createtime+=" 00:00:00";
        LocalDateTime nowTime = LocalDateTime.now();
        LocalDateTime cTime = LocalDateTime.parse(createtime, df);
        if(cTime.plusDays(7).isBefore(nowTime)){
            return ServerResponse.createByErrorMessage("超过7个工作日，不能修改");
        }

        String jsrq ="";
        jsrq =jsrq.substring(0,10);
        jsrq=jsrq+" 00:00:00";


        LocalDateTime settlementDate = LocalDateTime.parse(jsrq, df);
        jsrq += " 00:00:00";
        //时间单独传过来会报400,用String获取后自己加进去
        LocalDateTime ldt = LocalDateTime.parse(jsrq, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return null;
    }


    @Resource
    DealTableService dealTableService;
    @Resource
    GamesMapper  gamesMapper;
    @Resource
    BankCardMapper  bankCardMapper;
    @Resource
    BankByDealtMapper bankByDealtMapper;






    //显示今天全部班次的交易情况(没用到)
    @RequestMapping("/getAllOfToDayBypr")
    @ResponseBody
    public List getAllOfToDayBypr(){

        return dealTableService.getAllOfToDayBypr();
    }





    //跳转到卖分页面
    @RequestMapping("/toSellingScore")
    public String toSellingScore(Model model){
        Subject currentUser = SecurityUtils.getSubject();
        UserTable user=((UserTable) currentUser.getPrincipal());
        model.addAttribute("userID",user.getUid());
        model.addAttribute("userRName",user.getRelname());
        return "DealController/sellingScore";
    }




    //跳转到买分页面
    @RequestMapping("/toBuyScore")
    public String toBuyScore(Model model){
        Subject currentUser = SecurityUtils.getSubject();
        UserTable user=((UserTable) currentUser.getPrincipal());
        model.addAttribute("userID",user.getUid());
        model.addAttribute("userRName",user.getRelname());
        return "DealController/buyScore";
    }


    //买分
    @RequestMapping("/buyScore")
    @ResponseBody
    public ServerResponse buyScore(DealTable buyDeal,@RequestParam Map<String,String> map){
        int shift = TimeHandler.getShift();
        buyDeal.setShift(shift);
        return dealTableService.buyScore(buyDeal,map);
    }


    //跳转到卖分的首页
    @RequestMapping("/sellBusiness")
    @ResponseBody
    public ModelAndView sellBusiness(int Gid){
        ModelAndView mv = getBusiness(Gid);
        mv.setViewName("/DealController/sellBusiness");
        return mv;
    }
    //开始卖分
    @RequestMapping("/sellingScore")
    @ResponseBody
    public ServerResponse sellingScore( @RequestParam Map<String,String> map){

        return dealTableService.sellingScore2(map);
    }

    private ModelAndView getBusiness(int Gid) {
        ModelAndView mv =  new ModelAndView();
        Subject currentUser = SecurityUtils.getSubject();
        UserTable user=((UserTable) currentUser.getPrincipal());
        mv.addObject("userID",user.getUid());
        mv.addObject("userRName",user.getRelname());
        //现在时间是那一班
        int shift = TimeHandler.getShift();
        //现在工作日期
        LocalDate nowDay = LocalDate.now();
        if(shift == 0){
            mv.addObject("shift","早班");
        }
        if(shift == 1){
            mv.addObject("shift","中班");
        }
        if(shift == 2){
            nowDay = nowDay.plusDays(1);
            mv.addObject("shift","晚班");
        }
        mv.addObject("nowDay",nowDay);
        //找到游戏
        Games game = gamesMapper.selectByPrimaryKey(Gid);
        mv.addObject("game",game);
        //找到所有银行
        List<BankCard> bankList=bankCardMapper.getAll();
        mv.addObject("bankList",bankList);
        //找到本班已经交易的
        Map<String,String> map = new HashMap<>();
        Map<String,Object> mapO =TimeHandler.findParmHandler(map);
        mapO.put("Gid",Gid);
        List<BankByDealt> bankByDealtList= bankByDealtMapper.getAllByTime(mapO);
        List<BankByDealt> bankByDealtVOList = new ArrayList<>();
        for(BankCard b:bankList){
            BankByDealt bbdVo = new BankByDealt();
            bbdVo.setBid(b.getBid());
            bankByDealtVOList.add(bbdVo);
        }
        for(BankByDealt bbd:bankByDealtList){
            BankByDealt bvo= bankByDealtVOList.stream().filter(x -> x.getBid()==bbd.getBid()).findFirst().get();
            BeanUtils.copyProperties(bbd,bvo);
        }
        bankByDealtVOList.sort(Comparator.comparing(BankByDealt::getBid));
        mapO.put("bankByDealtVOList",bankByDealtVOList);
        return mv;
    }


}
